//
//  CustomerShakeRule.m
//  HuoBao
//
//  Created by 于会海 on 15/12/30.
//  Copyright © 2015年 jinxin. All rights reserved.
//

#import "CustomerShakeRule.h"
@interface CustomerShakeRule()
{
    UIView* dangView;
    UIView* backView;
    UIImageView* imageView;
    UIButton* cancelBtn;
    UITapGestureRecognizer* viewTap;
}
@end
@implementation CustomerShakeRule
#define GetFinalResult(x,y,z,r) (IsIphone4?x:(IsIphone6P?r:(IsIphone5?y:z)))
#define GetFinalresult(a,b) (isIphone4or5?a:b)
#define GetFinalImage(stringA,stringB,stringC,stringD) (IsIphone4?StringFormat(stringA):(IsIphone6P?StringFormat(stringD):(IsIphone5?StringFormat(stringB):StringFormat(stringC))))
#define StringFormat(a) [NSString stringWithFormat:@"%@",a]

- (instancetype)init{
    self = [super init];
    [self creatUI];

    return self;
}

- (void)creatUI{
    backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [backView setBackgroundColor:[UIColor clearColor]];
    [self addSubview:backView];
    
    dangView =  [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [dangView setBackgroundColor:[UIColor blackColor]];
    [dangView setAlpha:0.0];
    [backView addSubview:dangView];
    
    UIImage* image = [UIImage imageNamed:GetFinalImage(@"shake_rule_4", @"shake_rule_4", @"shake_rule_6", @"shake_rule_6")];
//    UIImage* image = [UIImage imageNamed:@"shake_rule_6" ];
    imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
    imageView.alpha = 0.3;
    imageView.image = image;
    LLog(@"imageView ----  %lf,,,%lf",imageView.size.width,imageView.size.height);
    imageView.centerX = dangView.centerX;
    imageView.centerY = GetFinalresult(dangView.centerY - 15, dangView.centerY);
    [backView addSubview:imageView];
    
    UIImage* btnNormalImage = [UIImage imageNamed:GetFinalImage(@"shake_rule_close_6", @"shake_rule_close_6", @"shake_rule_close_6", @"shake_rule_close_6")];
    UIImage* btnTapImage = [UIImage imageNamed:GetFinalImage(@"shake_rule_closeTap_6", @"shake_rule_closeTap_6", @"shake_rule_closeTap_6", @"shake_rule_closeTap_6")];
    cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH -22-btnNormalImage.size.width, imageView.origin.y+20, btnNormalImage.size.width, btnNormalImage.size.height)];
    cancelBtn.alpha = 0.3;
    [cancelBtn setBackgroundImage:btnNormalImage forState:UIControlStateNormal];
    [cancelBtn setBackgroundImage:btnTapImage forState:UIControlStateHighlighted];
    [backView addSubview:cancelBtn];
    [cancelBtn addTarget:self action:@selector(dismissRuleView) forControlEvents:UIControlEventTouchUpInside];
    
    self.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT);
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    viewTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [[UIApplication sharedApplication].keyWindow addGestureRecognizer:viewTap];
}

- (void)showShakeRule{
    if (self.isCancelRuleView){
        self.isCancelRuleView(NO);
    }
    [UIView animateWithDuration:0.35 animations:^{
        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.1 animations:^{
            dangView.alpha = 0.2;
            imageView.alpha = 0.6;
            cancelBtn.alpha = 0.6;
            
        } completion:^(BOOL finished) {
            dangView.alpha = 0.5;
            imageView.alpha = 1;
            cancelBtn.alpha = 1;
        }];
    }];
}

- (void)tapAction:(UITapGestureRecognizer*)tap{
    if (tap.state == UIGestureRecognizerStateEnded){
        CGPoint location = [tap locationInView:nil];
        if (![imageView pointInside:[imageView convertPoint:location fromView:imageView.window] withEvent:nil]){
            [self.window removeGestureRecognizer:viewTap];
            [self dismissRuleView];
        }
    }
}

- (void)dismissRuleView{
    [UIView animateWithDuration:0.1 animations:^{
        dangView.alpha = 0.1;
        imageView.alpha = 0.7;
        cancelBtn.alpha = 0.7;
    } completion:^(BOOL finished) {
        dangView.alpha = 0.0;
        imageView.alpha = 0.3;
        cancelBtn.alpha = 0.3;
    }];
    [UIView animateWithDuration:0.35 delay:0.2 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        if (self.isCancelRuleView) {
            self.isCancelRuleView (YES);
        }
        [self setFrame:CGRectMake(0, SCREEN_HEIGHT+20, SCREEN_WIDTH, SCREEN_HEIGHT)];
    } completion:^(BOOL finished) {
        [self.window removeGestureRecognizer:viewTap];
        [self removeFromSuperview];

    }];
}

@end
